package com.example.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.entity.Goods;
import com.example.entity.Order;
import com.example.exception.GoodsStockException;
import com.example.factory.OrderFactory;
import com.example.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	GoodServiceImpl goodServiceImpl;

	@Override
	public int insert(Order order) {
		
		return 0;
	}

	@Override
	public Order selectById(Long id) {
		return OrderFactory.getOrderFactory().getOrder()
				.setBuyGoodsNum(5L)
				.setUserName("张三");
	}

	@Override
	public synchronized void create(Order order) {
		// 查询商品数量
		Long goodsNum = goodServiceImpl.queryNumberById(order.getGoodsId());
		if (goodsNum < order.getBuyGoodsNum()) {
			throw new GoodsStockException("库存不足");
		}
		// 还要判断用户金额等条件并抛出异常。。。。。
		
		
		// 修改商品数量
		goodServiceImpl.update(new Goods().setNumber(goodsNum-order.getBuyGoodsNum()));
		// 生成订单
		insert(order);
	}

}
